package services

import (
	"encoding/json"
	"errors"
	"fmt"
	"hft/Database/admin"
	"hft/Http/Models/response"
	utils "hft/Http/Utils"
	"hft/global"
	maptool "hft/tools/map_tool"
)

type Map struct{}

// 关键词搜索

func (*Map) SearchTips(keyword string, city string, poiType string, location string) (interface{}, error) {
	var res, err = maptool.AmapApi.TipsSearch(keyword, city, poiType, true, location)

	var data *response.TipsSearchRes

	if err != nil {
		return nil, err
	}
	fmt.Println(string(res))
	err = json.Unmarshal(res, &data)

	if err != nil {
		return nil, err
	}

	return data.Tips, nil

}

func (*Map) GetCity(id int) (data *admin.Region, err error) {

	var region *admin.Region

	var city *admin.Region

	err = global.OC_DB.First(&region, id).Error

	if err != nil {
		return nil, errors.New("未知错误500")
	}

	var ids = utils.BelongMyCity(region.Link)

	err = global.OC_DB.Where("id IN ? AND level = ?", ids, "city").First(&city).Error

	if err != nil {
		return nil, errors.New("未知错误500")
	}

	return city, nil

}

var MapServices = new(Map)
